import { createSlice } from '@reduxjs/toolkit'
import axios from "axios";

const foodsStore = createSlice({
  name: 'foods',
  initialState: {
    foodsList: [],
    activeMenu: ''
  },
  reducers: {
    setFoodsList(state, action) {
      state.foodsList = action.payload;
    },
    setActiveMenu(state, action) {
      state.activeMenu = action.payload;
    }
  }
})

// 异步获取部分

const { setFoodsList, setActiveMenu } = foodsStore.actions;

const fetchFoodsList = () => {
  return async (dispatch) => {
    const res = await axios.get('http://localhost:3004/takeaway')
    dispatch(setFoodsList(res.data))
  }
}

export {
  fetchFoodsList,
  setActiveMenu
}

const reducer = foodsStore.reducer;

export default reducer